package plantshop.view;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.plaf.FontUIResource;

import plantshop.model.Game;
import plantshop.model.Opponent;
import plantshop.model.Player;
import plantshop.view.listeners.ScoreScreenMouseListener;


public class ScoreScreen extends JPanel {
	private static final long serialVersionUID = -8530315132535112387L;
	Image menu;
	Image title;
	public JFrame frame;
	Image background;
	Game game;
	Player player;
	int score;
	final static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	final static int frameWidth = screenSize.width;
   	final static int frameHeight = screenSize.height;
	public static final int MENU_HEIGHT = 900;
	public static final int MENU_WIDTH = 1500;
	public static final String MENU_TEXT = "Papyrus";
	public static final String BUTTON_TEXT = "Arial";
	public static final int[] TITLE_POSITION = new int[]{MENU_WIDTH/100,MENU_HEIGHT/120};//550,250
	public static final int[] SINGLE_POSITION = new int[]{MENU_WIDTH/2 - 15,300};
	public static final int[] MULTI_POSITION = new int[]{MENU_WIDTH/2 - 15,500};
	public static final int[] RESTART_POSITION = new int[]{MENU_WIDTH/2 - 15,700};
	public static final int BUTTON_HEIGHT = 70;
	public static final int BUTTON_WIDTH = 250;
	public static final int BUTTON_TEXT_SIZE = 36;
	public static final int TITLE_TEXT_SIZE = 120;
	public static final int[] SINGLE_TEXT_POSITION = new int[]{MENU_WIDTH/2,450};
	public static final int[] OPPONENT_TEXT_POSITION = new int[]{MENU_WIDTH/2 - 50,600};
	public static final int[] RESTART_TEXT_POSITION = new int[]{MENU_WIDTH/2,750};
	
	public ScoreScreen(Map<String, BufferedImage> images, JFrame frame, Game game, Player player, Map<String, BufferedImage[]> animations){
		this.background = images.get("Store");
		this.frame = frame;
		this.menu = images.get("Menu").getScaledInstance(MENU_WIDTH, MENU_HEIGHT, Image.SCALE_DEFAULT);
		this.title = images.get("Title");
		this.game = game;
		this.player = player;
		this.score = game.getStore().getMoney() + game.getStoreAttractiveness()/2;
		ScoreScreenMouseListener scoresListen = new ScoreScreenMouseListener(this, images, frame, animations);
        addMouseListener(scoresListen);
        addMouseMotionListener(scoresListen);
	}

	@Override
	public void paint(Graphics g) {
		int xpos = frame.getWidth()/20;
		int ypos = frame.getHeight()/20;
		
		//Draw Store
		g.drawImage(background, 0, 0, this);
		
		//Draw Menu
		g.drawImage(menu, xpos, ypos, this);
		g.setColor(Color.BLACK);
		g.setFont(new FontUIResource(new Font(MENU_TEXT,0,TITLE_TEXT_SIZE)));
		g.drawImage(title, xpos, ypos/50,this);
		
		//Draw Player Score
		g.setColor(Color.BLACK);
		g.setFont(new FontUIResource(new Font(BUTTON_TEXT,0,BUTTON_TEXT_SIZE)));
		g.drawString(player.getName()+" Score: " + score,SINGLE_TEXT_POSITION[0],SINGLE_TEXT_POSITION[1]);
		
		//Draw Opponents Scores
		if (player.getOpponents() != null){
			int count = 0;
			for(Opponent o: player.getOpponents()){
				g.drawString(o.getName()+" Score: " + (o.getMoney()+o.getRating()/2), SINGLE_TEXT_POSITION[0],SINGLE_TEXT_POSITION[1]+100*count);
				count++;
			}
		}
		
		//Draw Restart Button
		g.setColor(Color.GRAY);
		g.fillRect(RESTART_POSITION[0],RESTART_POSITION[1],BUTTON_WIDTH,BUTTON_HEIGHT);
		g.setColor(Color.BLACK);
		g.setFont(new FontUIResource(new Font(BUTTON_TEXT,0,BUTTON_TEXT_SIZE)));
		g.drawString("Play Again",RESTART_TEXT_POSITION[0],RESTART_TEXT_POSITION[1]);
	}
	
	public int[] getScoreButtonCoords(){
		return new int[]{SINGLE_POSITION[0], SINGLE_POSITION[1], BUTTON_WIDTH,BUTTON_HEIGHT,
						 MULTI_POSITION[0],MULTI_POSITION[1],BUTTON_WIDTH,BUTTON_HEIGHT,
						 RESTART_POSITION[0],RESTART_POSITION[1],BUTTON_WIDTH,BUTTON_HEIGHT};
	}
}
